<?php
namespace common\components;

use Yii;
use yii\db\ActiveRecord;
use common\components\behaviors\LogBehavior;
use common\components\behaviors\CacheBehavior;
use yii\web\ForbiddenHttpException;


/**
 * 自定义模型基类
 * 包涵图片上传行为，自定义img属性
 * @Description: 
 * @author: kai.gao
 * @date: 2014-11-21 上午10:31:06
 */
class ActiveRecord2015 extends ActiveRecord {
	
	public function init() {
            parent::init();
            $this->attachBehavior('log', [
                'class' => LogBehavior::className(),
            ]);
	}
	
	/**
	 * 加入回收站
	 * @fieldName: EVENT_RUBBISH
	 * @fieldType: unknown
	 * @Description:
	 */
	const EVENT_RUBBISH = 'rubbish';
	
	/**
	 * 恢复
	 * @fieldName: EVENT_RESTORE
	 * @fieldType: unknown
	 * @Description:
	 */
	const EVENT_RESTORE = 'restore';
	
	/**
	 * 批量修改
	 * @fieldName: EVENT_UPDATEALL
	 * @fieldType: unknown
	 * @Description:
	 */
	const EVENT_UPDATEALL = 'update_all';

	/**
	 * 批量删除
	 * @fieldName: EVENT_DELETEALL
	 * @fieldType: unknown
	 * @Description:
	 */
	const EVENT_DELETEALL = 'delete_all';
	
	public $changeModels;
	
	/**
	 * @Title: deleteAll
	 * @Description:重写父类的批量删除方法，触发delete_all事件 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-12-9 下午8:54:48
	 */
	public static function deleteAll($condition = '', $params = []) {
            $all_models = self::find()->where($condition, $params)->all();
            $rows = parent::deleteAll($condition, $params); 
            if ($rows > 0) {
                $static = new static;
                $static->changeModels = $all_models;
                $static->trigger(ActiveRecord2015::EVENT_DELETEALL);
            }
            return $rows;
	}
	
	/**
	 * @Title: updateAll
	 * @Description:重写父类的批量修改方法，触发update_all事件 
	 * @return: number
	 * @author: kai.gao
	 * @date: 2014-12-9 下午8:56:22
	 */
	public static function updateAll($attributes, $condition = '', $params = []) {
            $all_models = self::find()->where($condition, $params)->all();
            $rows = parent::updateAll($attributes, $condition, $params);

            if ($rows > 0) {
                $static = new static;
                $static->changeModels = $all_models;
                $static->trigger(ActiveRecord2015::EVENT_UPDATEALL);
            }
            return $rows;
	}
	 
	/**
	 * @Title: findOne
	 * @Description: 重写父类的findOne方法，如果有缓存直接返回结果，如果没有去差数据库 
	 * @return: mixed
	 * @author: kai.gao
	 * @date: 2014-12-9 下午8:56:55
	 */
	public static function findOne($condition) {
            return parent::findOne($condition);
	}

	
	
	/**
	 * 
	 * @Title: rubbish
	 * @Description: 加入回收站 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-11-24 下午5:40:30
	 */
	public function rubbish() {
            if ($this->hasAttribute('is_deleted')) {
                $this->off(ActiveRecord::EVENT_AFTER_UPDATE);
                $this->is_deleted = 1;
                if ($this->save())
                    $this->trigger(ActiveRecord2015::EVENT_RUBBISH);
            }
	}
	
	/**
	 * 
	 * @Title: restore
	 * @Description: 恢复操作 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2014-11-24 下午5:40:44
	 */
	public function restore() {
            if ($this->hasAttribute('is_deleted')) {
                $this->off(ActiveRecord::EVENT_AFTER_UPDATE);
                $this->is_deleted = 0;
                if ($this->save())
                $this->trigger(ActiveRecord2015::EVENT_RESTORE);
            }
	}
}